<template>
    <el-dialog
      :visible.sync="visible"
      :title="commissionModel === 1 ? '选择下级修改团队返点比例修改方式' : '选择下级佣金比例修改方式'"
      width="1200px"
      @close="handleReset"
      append-to-body
    >
        <Content ref="Content" v-if="visible" :agentData="agentData" :agentPoint="agentPoint" :commissionModel="commissionModel" />
        <div slot="footer" class="dialog-footer">
            <el-button type="default" @click="handleReset">
               取消
            </el-button>
            <el-button :loading="loading" type="primary" @click="handleOk">
               确认
            </el-button>
			</div>
    </el-dialog>
  </template>
<script>
import Content from './content.vue'
export default {
    name: 'AgentPointSetAllSub',
    components: {
        Content
    },
    props: {
        loading: {
            type: Boolean,
            default: false
        },
        visible: {
            type: Boolean,
            default: false
        },
        // eslint-disable-next-line vue/require-default-prop
        agentData: {
            type: Object
        },
        getPoints: {
            type: Function,
            default: undefined
        },
        commissionModel: {
            type: Number,
        }
    },
    data() {
        return {
            agentPoint: {}
        }
    },
    watch: {
        visible: {
            handler(bool) {
                if (bool) {
                    this.agentPoint = this.formatPoints()
                }
            }
        }
    },
    methods: {
        handleOk() {
            const vm = this.$refs['Content']
            this.$emit('ok', {adjustmentType: vm.adjustmentType})
            console.log('commissionRateUpdate', '-------')
        },
        handleReset() {
            this.$emit('close')
        },
        formatPoints() {
            const points = {}
            const pointArrs = this.getPoints(
                this.commissionModel === 1 ?  ['teamRebates', 'teamContribution'] :  ['commissionDzRebates']
            )
            
            if (this.commissionModel === 1) {
                pointArrs.forEach(point => {
                    for (const key in point) {
                        if (key.endsWith('Rebate')) {
                            let k = key
                            if (point.zhanchengFlag === 1) {
                                k = k.replace('Rebate', 'Zhancheng')
                            }
                            points[k] = point[key]
                        }
                    }
                })
            }

            if (this.commissionModel === 2) {
                pointArrs.forEach(point => {
                    for (const key in point) {
                        if (key.endsWith('Commission')) {
                            let k = key
                            points[k] = point[key]
                        }
                    }
                })
            }
            
            return points
        }
    }

}
</script>

<style scoped>
.dialog-footer{
    text-align: center;
}
</style>
